package com.example.shopping.fpage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shopping.GoodsTypeActivity;
import com.example.shopping.R;
import com.example.shopping.goods.TypeBean;
import com.example.shopping.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class FPTypeAdapter extends BaseAdapter {
    Context context;
    List <TypeBean.DataBean>data;
    public  FPTypeAdapter(Context context,List<TypeBean.DataBean>list,int page,int pagesize){
        this.context=context;
        data=new ArrayList<>();
        //page*pagesize~(page+1)*paesize   0-10
        int start=page*pagesize;//第一个角标
        int end=(page+1)*pagesize-1;//最后一个角标
         if (end>list.size()-1){
             end=list.size()-1;
         }
         for (int i=start;i<=end;i++){
             TypeBean.DataBean bean=list.get(i);
             data.add(bean);

         }
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView= LayoutInflater.from(context).inflate(R.layout.item_fp_type,null);
        TextView tv=convertView.findViewById(R.id.item_fp_type_tv);
        ImageView iv=convertView.findViewById(R.id.item_fp_type_iv);
        TypeBean.DataBean bean=data.get(position);
        tv.setText(bean.getName());
        String picUrl= URLUtils.PUBLIC_URL+bean.getIcon();
        Picasso.with(context).load(picUrl).into(iv);
        setClickListener(convertView,bean);
        return convertView;
    }

    private void setClickListener(View v, final TypeBean.DataBean bean) {
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, GoodsTypeActivity.class);
                intent.putExtra("cid",bean.getId());
                context.startActivity(intent);
            }
        });
    }
}
